package com.ptswitch.material.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxzon.project.EHibernateDaoSupport;
import com.hxzon.util.StringUtil;
import com.ptswitch.material.dao.BomDao;
import com.ptswitch.material.model.Bom;

@Repository("bomDao")
public class BomHibernate extends EHibernateDaoSupport<Bom> implements BomDao {

	public Bom getByCode(String code, String version) {
		code = StringUtil.escapeSql(code);
		version = StringUtil.escapeSql(version);
		List<Bom> list = getList(" where p.code='" + code + "' and p.version='" + version + "'", -1, -1);
		if (list.isEmpty()) {
			return null;
		}
		if (list.size() > 1) {
			logger.error("bom [{}-{}] not unique", code, version);
		}
		return list.get(0);
	}

	public String orderBy() {
		return " order by p.code ";
	}

}
